import { ProForm, ProFormText, ProFormSelect } from '@ant-design/pro-components';
import { Col, Row, Space, message } from 'antd';
import { addUser, changeRole } from '@/services/userManagement'
import type { PatientsItem } from '@/interfaces/patients'
import { STATUS_CODE } from '@/common/const'
import role_enum from './role';

const AddForm = ({ onFinish }) => {
    return (
        <ProForm< Partial<PatientsItem>>
            labelCol={{ span: 8 }}
            wrapperCol={{ span: 14 }}
            layout='horizontal'
            submitter={{
                render: (props, doms) => {
                    return <Row>
                        <Col span={4} offset={20} >
                            <Space>{doms}</Space>
                        </Col>
                    </Row>
                },
            }}
            onFinish={async (values) => {
                try {
                    const data = await addUser(values)
                    if (data.code === STATUS_CODE.SUCCESS) {
                        onFinish()
                        message.success('提交成功');
                    } else {
                        message.error(data.message);
                    }
                } catch (error) {
                    message.error('提交失败');
                }

            }}
        >
            <ProFormText
                width="md"
                name="jobnumber"
                label="工号"
                placeholder="请输入工号"
                colProps={{ span: 12 }}
                rules={[{ required: true }]}
            />
            <ProFormText
                width="md"
                name="username"
                label="姓名"
                placeholder="请输入姓名"
                colProps={{ span: 12 }}
                rules={[{ required: true }]}
            />
            <ProFormSelect
                colProps={{ span: 12 }}
                label="角色"
                name="role"
                width="md"
                valueEnum={role_enum}
                rules={[{ required: true }]}
            />
            <ProFormText
                width="md"
                name="phone"
                label="手机号"
                placeholder="请输入手机号"
                colProps={{ span: 12 }}
                rules={[{
                    pattern: /^1\d{10}$/,
                    message: '请输入正确的手机号'
                }]}
            />
        </ProForm>
    )
}

const ChangeRole = ({ onFinish, id }) => {
    return (
        <ProForm< Partial<PatientsItem>>
            labelCol={{ span: 8 }}
            wrapperCol={{ span: 14 }}
            layout='horizontal'
            submitter={{
                render: (props, doms) => {
                    return <Row>
                        <Col span={4} offset={20} >
                            <Space>{doms}</Space>
                        </Col>
                    </Row>
                },
            }}
            onFinish={async (values) => {
                try {
                    const data = await changeRole(id, values)
                    if (data.code === STATUS_CODE.SUCCESS) {
                        onFinish()
                        message.success('提交成功');
                    } else {
                        message.error(data.message);
                    }
                } catch (error) {
                    message.error('提交失败');
                }
            }}
        >
            <ProFormSelect
                colProps={{ span: 12 }}
                label="角色"
                name="role"
                width="md"
                valueEnum={role_enum}
                rules={[{ required: true }]}
            />

        </ProForm>
    )
}

export {
    AddForm,
    ChangeRole
} 